package com.jinmdz.fmis.api.global;

import com.jinmdz.fmis.api.model.config.ConfigApp;
import com.jinmdz.fmis.common.ext.ArrayListExt;
import com.jinmdz.fmis.core.constant.Constants;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.*;

import javax.annotation.Resource;

/**
 * 全局WebMvc配置器
 *
 * @author LiCongLu
 * @date 2019-12-20 14:34
 */
@Configuration
public class GlobalWebMvcConfigurer implements WebMvcConfigurer {

    @Resource
    private ConfigApp configApp;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 不拦截路径
        ArrayListExt<String> excludePaths = new ArrayListExt<>();
        excludePaths.addExt("/swagger-ui.html", "/swagger-resources/**", "/webjars/springfox-swagger-ui/**")
                .addExt("*.js", "/**/*.js", "*.css", "/**/*.css", "*.html", "/**/*.html")
                .addExt("/druid/").addExt("/druid/**")
                // 不拦截加载系统列表接口
                .addExt("/api/user/load-system-list")
                // 不拦截登录接口
                .addExt("/api/user/login")
                .addExt("/test/").addExt("/test/**")
                .addExt(configApp.getFileUrlPath() + "/**")
                .addExt(Constants.ERROR_PATH);

        // 用户排除拦截，多个拦截器组成一个拦截器链
        registry.addInterceptor(globalHandlerInterceptor())
                // 用于添加拦截规则
                .addPathPatterns("/**")
                // 不拦截静态资源
                .excludePathPatterns(excludePaths.toArray(new String[]{}));
    }

    @Bean
    public GlobalHandlerInterceptor globalHandlerInterceptor() {
        return new GlobalHandlerInterceptor();
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        // addMapping 允许跨域访问的路径
        registry.addMapping("/**")
                // 允许跨域访问的源
                .allowedOrigins("*")
                // 允许请求方法
                .allowedMethods("POST", "GET")
                // maxAge 预检间隔时间
                .maxAge(168000)
                // 允许头部设置
                .allowedHeaders("*")
                // 是否发送cookie
                .allowCredentials(true);
    }

    @Bean
    public CorsFilter corsFilter() {
        // 过滤器拦截跨域问题
        CorsConfiguration config = new CorsConfiguration();
        config.addAllowedOrigin("*");
        config.setAllowCredentials(true);
        config.addAllowedMethod("*");
        config.addAllowedHeader("*");
        UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();
        configSource.registerCorsConfiguration("/**", config);
        return new CorsFilter(configSource);
    }

    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        AntPathMatcher matcher = new AntPathMatcher();
        // 取消URL地址区分大小
        matcher.setCaseSensitive(false);
        configurer.setPathMatcher(matcher);
    }
}
